package com.hmurphy.android.batterymeter;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.RemoteViews;

public class BatteryPercentAppWidgetProvider extends AppWidgetProvider {
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		Intent batteryIntent = 
			context.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
		int rawlevel = batteryIntent.getIntExtra("level", -1);
		double scale = batteryIntent.getIntExtra("scale", -1);
		double level = -1;
		if (rawlevel >= 0 && scale > 0) { level = rawlevel / scale;}
		level *= 100;
		
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.battery_meter_appwidget);
        views.setTextViewText(R.id.txtPercent, Double.toString(level) + "%");

        appWidgetManager.updateAppWidget(appWidgetIds[0], views);
	}
}